<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header style="background-color: #0096dc">
            <h1 style="font-size: 22px;color: white">
                CoolShark商城后台管理系统
                <span v-if="user.nickname!=null" style="float: right;font-size: 15px">
          欢迎{{user.nickname}}回来
          <a href="javascript:void(0)" @click="logout()">退出登录</a>
        </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside width="200px" style="overflow: hidden">
                <!--侧栏开始-->
                <el-menu @select="handleSelect" :default-active="currentIndex">
                    <el-submenu index="1">
                        <template slot="title">
                            <i class="el-icon-s-flag"></i>
                            <span>分类管理</span>
                        </template>
                        <el-menu-item index="1-1">分类列表</el-menu-item>
                        <el-menu-item index="1-2">添加分类</el-menu-item>
                    </el-submenu>
                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图</span>
                        </template>
                        <el-menu-item index="2-1">轮播图列表</el-menu-item>
                        <el-menu-item index="2-2">添加轮播图</el-menu-item>
                    </el-submenu>
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-shopping-cart-2"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item index="3-1">商品列表</el-menu-item>
                        <el-menu-item index="3-2">添加商品</el-menu-item>
                    </el-submenu>
                </el-menu>
                <!--侧栏结束-->
            </el-aside>
            <el-main>
                <!--分类表格开始-->
                <el-table v-if="currentIndex=='1-1'" :data="categoryArr">
                    <el-table-column type="index" label="编号" width="200"></el-table-column>
                    <el-table-column prop="name" label="分类名称" width="200"></el-table-column>
                    <el-table-column label="操作">
                        <!--slot-scope="scope" 定义了一个scope变量,
                        在此变量中保存着当前行的相关信息
                        scope.$index 得到当前行对应的下标
                        scope.row得到的是当前行对应的数组里面的对象
                        此处的template必须添加
                        -->
                        <template slot-scope="scope">
                            <el-button size="mini" type="success"
                                       @click="categoryUpdate(scope.row)">修改
                            </el-button>
                            <el-popconfirm @confirm="categoryDelete(scope.$index, scope.row)"
                                           title="您确认删除此分类吗？">
                                <el-button slot="reference" size="mini"
                                           type="danger">删除
                                </el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分类表格结束-->
                <!--轮播图表格开始-->
                <el-table v-if="currentIndex=='2-1'" :data="bannerArr">
                    <el-table-column type="index" label="编号" width="200"></el-table-column>
                    <el-table-column label="轮播图" width="300">
                        <!--自定义列 显示轮播图图片 必须添加template-->
                        <template slot-scope="scope">
                            <!--scope.row代表当前行对应的数据里面的对象-->
                            <img :src="scope.row.url" width="100%">
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" type="success"
                                       @click="bannerUpdate(scope.row)">修改
                            </el-button>
                            <el-popconfirm @confirm="bannerDelete(scope.$index, scope.row)"
                                           title="您确认删除此轮播图吗？">
                                <el-button slot="reference" size="mini"
                                           type="danger">删除
                                </el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!--轮播图表格结束-->
                <!--商品表格开始-->
                <el-table v-if="currentIndex=='3-1'" :data="productArr">
                    <el-table-column type="index" label="编号" width="100"></el-table-column>
                    <el-table-column prop="title" label="商品标题"></el-table-column>
                    <el-table-column prop="price" label="商品价格"></el-table-column>
                    <el-table-column prop="oldPrice" label="商品原价"></el-table-column>
                    <el-table-column prop="saleCount" label="商品销量"></el-table-column>
                    <el-table-column prop="num" label="商品库存"></el-table-column>
                    <el-table-column prop="categoryName" label="分类名称"></el-table-column>
                    <el-table-column label="商品图片" width="80">
                        <!--自定义列 显示轮播图图片 必须添加template-->
                        <template slot-scope="scope">
                            <!--scope.row代表当前行对应的数据里面的对象-->
                            <img :src="scope.row.url" width="100%">
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button size="mini" type="success"
                                       @click="productUpdate(scope.row)">修改
                            </el-button>
                            <el-popconfirm @confirm="productDelete(scope.$index, scope.row)"
                                           title="您确认删除此商品吗？">
                                <el-button slot="reference" size="mini"
                                           type="danger">删除
                                </el-button>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!--商品表格结束-->

            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                currentIndex: "1-1",
                categoryArr: [],
                bannerArr: [],
                productArr: [],
                user:{}
            }
        },
        methods: {
            logout(){
              if (confirm("您确认退出登录吗?")){
                  axios.get("/logout").then(function () {
                      location.href = "/";
                  })
              }
            },
            productUpdate(product){
              location.href = "/updateProduct.html?id="+product.id;
            },
            productDelete(i,product){
              //发出删除的请求
                axios.get("/product/delete?id="+product.id).then(function () {
                    //删除数组中的数据 让页面跟着响应
                    v.productArr.splice(i,1);
                    v.$message.success("删除成功!");
                })
            },
            bannerUpdate(banner) {
                //跳转到修改轮播图页面
                location.href="/updateBanner.html?id="+banner.id;
            },
            categoryUpdate(category) {
                let name = prompt("请输入新的分类名称!");
                //如果用户点击了取消 name=null
                if (name == null) {
                    return;
                }
                if (name.trim() == "") {
                    v.$message.error("分类名称不能为空!");
                    return;
                }
                //准备一个对象封装分类的数据
                let c = {id: category.id, name: name};
                //发出修改请求
                axios.post("/category/update", c).then(function () {
                    //修改页面
                    category.name = name;
                })


            },
            categoryDelete(i, category) {
                //发出删除分类的请求
                axios.get("/category/delete?id=" + category.id).then(function () {
                    //删除数组里面的数据
                    v.categoryArr.splice(i, 1);
                    v.$message("删除了分类:" + category.name);
                })

            },
            bannerDelete(i, banner) {
                //发出删除请求
                axios.get("/banner/delete?id=" + banner.id).then(function () {
                    //页面删除
                    v.bannerArr.splice(i, 1);
                    v.$message("删除轮播图成功!");
                })


            },
            handleSelect(key, keyPath) {
                if (key == "1-2") {
                    let name = prompt("请输入分类名称!");
                    if (name == null) {
                        return;
                    }
                    if (name.trim() == "") {
                        v.$message.error("分类名称不能为空!");
                        return;
                    }
                    axios.get("/category/insert?name=" + name).then(function (response) {
                        //添加完新的分类后如果刷新页面(负担太重需要请求更多的数据),可以通过
                        //让服务器响应所有分类数组的方式 解决此问题
                        v.categoryArr = response.data;
                    })

                } else if (key == "2-2") {
                    //跳转到添加轮播图的页面
                    location.href = "insertBanner.html";
                } else if (key == "3-2") {
                    location.href = "insertProduct.html"
                } else {
                    v.currentIndex = key;
                }
            }
        },
        created: function () {
            //判断是否包含   ?currentIndex=2-1
            if (location.search.indexOf("currentIndex")!=-1){//包含
                this.currentIndex = location.search.split("=")[1];
            }

            //获取分类数据
            axios.get("/category/select").then(function (response) {
                v.categoryArr = response.data;
            });
            //获取轮播图数据
            axios.get("/banner/select").then(function (response) {
                v.bannerArr = response.data;
            });
            //获取所有商品数据
            axios.get("/product/select").then(function (response) {
               v.productArr = response.data;
            })
            //发请求获取当前登录的用户对象
            axios.get("/currentUser").then(function(response){
                v.user = response.data;
                //如果没有登录的话会话对象里面得到的是个null 但是传递 到客户端这边后变成了空字符串
                if (v.user==""){
                    alert("请先登录!")
                    location.href="/login.html";
                }
            })
        }
    })
</script>
</html>